﻿using System;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Windows.Forms;
using EncryptionLib;
using EncryptionLib.Encryption;
using EncryptionLib.Signature;
using EncryptionLib.Users;


namespace TotalCommander_v3
{
    public partial class frmVerifySignature : Form
    {
        public frmVerifySignature()
        {
            InitializeComponent();
        }

        private void frmVerifySignature_Load(object sender, EventArgs e)
        {

        }

        private void btnVerify_Click(object sender, EventArgs e)
        {
            string sDateFile = txtSignInputFile.Text;
            if (String.IsNullOrEmpty(sDateFile))
            {
                MessageBox.Show("Original file can not be null or empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string sSignFile = txtSignSignFile.Text;
            if (String.IsNullOrEmpty(sSignFile))
            {
                MessageBox.Show("Signature file can not be null or empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                for (int i = frmMain.m_userManager.GetCount() - 1; i >= 0; --i)
                {
                    UserData user = frmMain.m_userManager.GetUser(i);
                    if (Signer.Verify(user, sDateFile, sSignFile))
                    {
                        MessageBox.Show(this,
                            "Verify successful!.\r\n This file was signed by " + user.Email,
                            "Congrats", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                MessageBox.Show(this, "Cannot verify this signature :(",
                    "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(this, "Error:\r\n" + ex.Message, "Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OpenFileDialog fDlg = new OpenFileDialog();
            fDlg.Filter = "All files (*.*)|*.*";
            fDlg.Title = "Type File";
            fDlg.CheckFileExists = true;
            fDlg.CheckPathExists = true;

            if (fDlg.ShowDialog() == DialogResult.OK)
            {
                if (fDlg.FileName == "")
                    return;
                txtSignInputFile.Text = fDlg.FileName;

            }
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OpenFileDialog fDlg = new OpenFileDialog();
            fDlg.Filter = "Sign files (*.sig)|*.sig|All files (*.*)|*.*";
            fDlg.Title = "Type File";
            fDlg.CheckFileExists = true;
            fDlg.CheckPathExists = true;

            if (fDlg.ShowDialog() == DialogResult.OK)
            {
                if (fDlg.FileName == "")
                    return;
                txtSignSignFile.Text = fDlg.FileName;

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
